package com.geese.module.admin.service.impl;

import com.geese.common.constants.BusinessConstants;
import com.geese.common.constants.Constants;
import com.geese.common.entity.Business;
import com.geese.common.entity.TransferRecordDO;
import com.geese.common.util.BeanUtil;
import com.geese.common.vo.Page;
import com.geese.exception.BusinessException;
import com.geese.module.admin.dto.TransferDTO;
import com.geese.module.admin.service.ITransferService;
import com.geese.module.business.dao.BusinessMapper;
import com.geese.module.business.dao.TransferRecordMapper;
import com.geese.module.business.vo.TransferRecordVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import ma.glasnost.orika.metadata.Type;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by huweiwei on 2017/3/27.
 */
@Service
public class TransferServiceImpl implements ITransferService{

    @Autowired
    private TransferRecordMapper transferRecordMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public Map<String, Object> selectList(Page page, TransferRecordVO transferVO) {
        PageHelper.startPage(page.getCurrPage(), page.getPageSize(), " id desc");
        List<TransferRecordDO> list = transferRecordMapper.selectList(transferVO);
        Type<TransferRecordDO> source = BeanUtil.getType(TransferRecordDO.class);
        Type<TransferDTO> target = BeanUtil.getType(TransferDTO.class);
        List<TransferDTO> data = BeanUtil.mapList(list, source, target);
        if (CollectionUtils.isNotEmpty(list)) {
            if (transferVO.getBusinessId() != null) {
                Business business = businessMapper.selectByPrimaryKey(transferVO.getBusinessId());
                if (business != null) {
                    String name = business.getName();
                    String mobile = business.getMobile();
                    for (TransferDTO transferDTO : data) {
                        transferDTO.setBusinessName(name);
                        transferDTO.setBusinessMobile(mobile);
                    }
                }
            } else {
                List<Integer> businessIds = Lists.transform(data, new Function<TransferDTO, Integer>() {
                    @Override
                    public Integer apply(TransferDTO transferDTO) {
                        return transferDTO.getBusinessId();
                    }
                });

                List<Business> businessList = businessMapper.selectByIdsIgnoreDel(businessIds);
                Map<Integer, Business> mapOfId = Maps.uniqueIndex(businessList, new Function<Business, Integer>() {
                    @Override
                    public Integer apply(Business business) {
                        return business.getId();
                    }
                });

                for (TransferDTO transferDTO : data) {
                    Business business = mapOfId.get(transferDTO.getBusinessId());
                    transferDTO.setBusinessName(business.getName());
                    transferDTO.setBusinessMobile(business.getMobile());
                }
            }
        }
        PageInfo pages = new PageInfo(list);
        Map<String, Object> pageInfo = Maps.newHashMap();
        pageInfo.put(Constants.CURR_PAGE, pages.getPageNum());
        pageInfo.put(Constants.TOTAL_PAGE, pages.getPages());
        pageInfo.put(Constants.TOTAL, pages.getTotal());
        Map<String, Object> result = Maps.newHashMap();
        result.put(Constants.PAGE_INFO, pageInfo);
        result.put("transferList", data);
        return result;
    }

    @Override
    public Integer doConfirm(int id) {
        TransferRecordDO transferRecordDO = transferRecordMapper.findById(id);
        if (null == transferRecordDO) {
            throw new BusinessException(120105);
        }
        if (transferRecordDO.getStatus() != BusinessConstants.TRANSFER_RECORD_STATUS_PAY) {
            throw new BusinessException(120106);
        }
        transferRecordDO.setStatus(BusinessConstants.TRANSFER_RECORD_STATUS_CONFIRM);
        int skip = transferRecordMapper.updateRecord(transferRecordDO);
        if (skip != 1) {
            throw new BusinessException(120034);
        }
        return transferRecordDO.getTransferNumber();
    }

    @Override
    public Integer doRemind(int id) {
        TransferRecordDO transferRecordDO = transferRecordMapper.findById(id);
        // TODO: 2017/3/28 提醒打款
        return transferRecordDO.getTransferNumber();
    }
}
